home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / CheckboxGroup.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.1 KB  |  136 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)CheckboxGroup.java    1.24 98/09/21
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. /**
  17.  * The <code>CheckboxGroup</code> class is used to group together 
  18.  * a set of <code>Checkbox</code> buttons. 
  19.  * <p>
  20.  * Exactly one check box button in a <code>CheckboxGroup</code> can 
  21.  * be in the "on" state at any given time. Pushing any 
  22.  * button sets its state to "on" and forces any other button that 
  23.  * is in the "on" state into the "off" state. 
  24.  * <p>
  25.  * The following code example produces a new check box group,
  26.  * with three check boxes: 
  27.  * <p>
  28.  * <hr><blockquote><pre>
  29.  * setLayout(new GridLayout(3, 1));
  30.  * CheckboxGroup cbg = new CheckboxGroup();
  31.  * add(new Checkbox("one", cbg, true));
  32.  * add(new Checkbox("two", cbg, false));
  33.  * add(new Checkbox("three", cbg, false));
  34.  * </pre></blockquote><hr>
  35.  * <p>
  36.  * This image depicts the check box group created by this example:
  37.  * <p>
  38.  * <img src="doc-files/CheckboxGroup-1.gif"
  39.  * ALIGN=center HSPACE=10 VSPACE=7> 
  40.  * <p>
  41.  * @version     1.24 09/21/98
  42.  * @author     Sami Shaio
  43.  * @see         java.awt.Checkbox
  44.  * @since       JDK1.0
  45.  */
  46. public class CheckboxGroup implements java.io.Serializable {
  47.     /**
  48.      * The current choice.
  49.      * @serial
  50.      * @see getCurrent()
  51.      * @see setCurrent()
  52.      */
  53.     Checkbox selectedCheckbox = null;
  54.  
  55.     /*
  56.      * JDK 1.1 serialVersionUID 
  57.      */
  58.     private static final long serialVersionUID = 3729780091441768983L;
  59.  
  60.     /**
  61.      * Creates a new instance of <code>CheckboxGroup</code>. 
  62.      */
  63.     public CheckboxGroup() {
  64.     }
  65.  
  66.     /**
  67.      * Gets the current choice from this check box group.
  68.      * The current choice is the check box in this  
  69.      * group that is currently in the "on" state, 
  70.      * or <code>null</code> if all check boxes in the
  71.      * group are off.
  72.      * @return   the check box that is currently in the
  73.      *                 "on" state, or <code>null</code>.
  74.      * @see      java.awt.Checkbox
  75.      * @see      java.awt.CheckboxGroup#setSelectedCheckbox
  76.      * @since    JDK1.1
  77.      */
  78.     public Checkbox getSelectedCheckbox() {
  79.     return getCurrent();
  80.     }
  81.  
  82.     /**
  83.      * @deprecated As of JDK version 1.1,
  84.      * replaced by <code>getSelectedCheckbox()</code>.
  85.      */
  86.     public Checkbox getCurrent() {
  87.     return selectedCheckbox;
  88.     }
  89.  
  90.     /**
  91.      * Sets the currently selected check box in this group
  92.      * to be the specified check box.
  93.      * This method sets the state of that check box to "on" and 
  94.      * sets all other check boxes in the group to be off.
  95.      * <p>
  96.      * If the check box argument is <code>null</code> or belongs to a 
  97.      * different check box group, then this method does nothing. 
  98.      * @param     box   the <code>Checkbox</code> to set as the
  99.      *                      current selection.
  100.      * @see      java.awt.Checkbox
  101.      * @see      java.awt.CheckboxGroup#getSelectedCheckbox
  102.      * @since    JDK1.1
  103.      */
  104.     public void setSelectedCheckbox(Checkbox box) {
  105.         setCurrent(box);
  106.     }
  107.  
  108.     /**
  109.      * @deprecated As of JDK version 1.1,
  110.      * replaced by <code>setSelectedCheckbox(Checkbox)</code>.
  111.      */
  112.     public synchronized void setCurrent(Checkbox box) {
  113.     if (box != null && box.group != this) {
  114.         return;
  115.     }
  116.     Checkbox oldChoice = this.selectedCheckbox;
  117.     this.selectedCheckbox = box;
  118.     if ((oldChoice != null) && (oldChoice != box)) {
  119.         oldChoice.setState(false);
  120.     }
  121.     if (box != null && oldChoice != box && !box.getState()) {
  122.         box.setStateInternal(true);
  123.     }
  124.     }
  125.  
  126.     /**
  127.      * Returns a string representation of this check box group,
  128.      * including the value of its current selection.
  129.      * @return    a string representation of this check box group.
  130.      */
  131.     public String toString() {
  132.     return getClass().getName() + "[selectedCheckbox=" + selectedCheckbox + "]";
  133.     }
  134.  
  135. }
  136.